package com.example.kafka_demo.kafka;

import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.annotation.PartitionOffset;
import org.springframework.kafka.annotation.TopicPartition;
import org.springframework.kafka.support.Acknowledgment;
import org.springframework.stereotype.Component;

import java.util.Optional;

@Component
public class KafkaConsumer {
//    private final Logger logger = LoggerFactory.getLogger(KafkaConsumer.class);

    //不指定group，默认取yml里配置的
    @KafkaListener(id= "twd_local_test",groupId= "twd_local_test",
            topicPartitions = {@TopicPartition(topic = "tlx-es-test",
                    partitionOffsets = {
                            @PartitionOffset(partition = "3", initialOffset = "1261122")
                    })})
    public void onMessage1(ConsumerRecord<String, String> record, final Acknowledgment ack) {
        Optional<String> kafkaMessage = Optional.ofNullable(record.value());
        // Kafka send 未实现 Serializable 的实体 此处会接收到空的 Optional
        if (kafkaMessage.isPresent()) {
            System.out.println(kafkaMessage.get());
            // 发送 ACK
            ack.acknowledge();
        }
    }
}